package com.ymatou.userservice.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userservice.parameter.BindWeiXinUseroRequestDtoBean;
import com.ymatou.userservice.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.userservice.parameter.WeiXinUserRequestDtoBean;
import com.ymatou.userservice.service.BindWeiXinUseroRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.sqlwapper.YmtUsersWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.AppTypeChannel;
import com.ymttest.utils.constant.ECode;
import com.ymttest.utils.constant.ThirdPartyType;
/**
*绑定微信账号接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_BindWeiXinUseroRequestDto{
	private static BindWeiXinUseroRequestDtoBean bindweixinuserorequestdtoBean;
	private static BindWeiXinUseroRequestDtoCall bindweixinuserorequestdtoCall;
	private static YmtUsersWapper ymtUser;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("绑定微信账号接口");
		ymtUser = new YmtUsersWapper();
	}

	@Before
	public void caseUp() {
		bindweixinuserorequestdtoBean=new BindWeiXinUseroRequestDtoBean();
		bindweixinuserorequestdtoCall=new BindWeiXinUseroRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(bindweixinuserorequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindWeiXinUseroRequestDto_001() {
		Logger.start(true,"微信账号未绑定过，绑定成功");
		try {
			String unionId = "NewBindWeixin_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map weixinUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_Wexin_Identify", "sUnionId", unionId);
			Logger.verifyIsNull(weixinUser, "数据库中不存在该第三方账号");
			
			Logger.comment("前置准备：新创建一个用户");
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			
			Map result = bindWeiXin(token, "mainsite", unionId);
			Logger.verifyEquals(ECode.SUCCESS, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
			Logger.verifyEquals(true, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证结果");
			
			Logger.comment("验证该账号不需要绑定手机");
			UserCallService.checkThirdPartyNeedBindMobile(unionId, ThirdPartyType.WEIXIN, ECode.USER_NO);
			Logger.comment("验证用该账号登陆成功");
			Map loginUser = UserCallService.regByWeiXin(unionId, "",true, "登录成功");
			Logger.verifyEquals((Integer)user.get("userId"), ((Double)loginUser.get("userId")).intValue(), "验证绑定的是一个用户");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindWeiXinUseroRequestDto_002() {
		Logger.start(true,"微信账号绑定过当前账号，已绑定过");
		try {
			String unionId = "NewBindWeixin_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map weixinUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_Wexin_Identify", "sUnionId", unionId);
			Logger.verifyIsNull(weixinUser, "数据库中不存在该第三方账号");
			
			Logger.comment("前置准备：新创建一个用户");
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			
			Map result = bindWeiXin(token, "mainsite", unionId);	
			Logger.verifyEquals(ECode.SUCCESS, String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			
			Logger.comment("再次绑定");
			result = bindWeiXin(token, "mainsite", unionId);
			Logger.verifyEquals("401", String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			Logger.verifyEquals(false, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("", result.get("Result"), "验证结果");
			Logger.verifyEquals("此微信账号已绑定", result.get("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindWeiXinUseroRequestDto_003() {
		Logger.start(true,"微信账号已绑定过其他账号，绑定失败");
		try {
			String unionId = "NewBindWeixin_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map weixinUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_Wexin_Identify", "sUnionId", unionId);
			Logger.verifyIsNull(weixinUser, "数据库中不存在该第三方账号");
			Logger.comment("用该账号注册一个新账号");
			UserCallService.regByWeiXin(unionId, TestDataManager.getPhoneNum(), true, "注册成功");
			
			Logger.comment("在另一个码头账号里绑定该微信");
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			
			Map result = bindWeiXin(token, "mainsite", unionId);
			Logger.verifyEquals("401",  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			Logger.verifyEquals(false, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("", result.get("Result"), "验证结果");
			Logger.verifyEquals("此微信账号已绑定", result.get("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindWeiXinUseroRequestDto_004() {
		Logger.start(true,"用户绑定了A微信账号->解绑A微信账号 ->用A登录失败 ->绑定A微信账号->绑定成功 ->用A登录成功");
		try {
			String unionId = "NewBindWeixinA_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map weixinUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_Wexin_Identify", "sUnionId", unionId);
			Logger.verifyIsNull(weixinUser, "数据库中不存在该第三方账号");
			
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			int userId = (Integer)user.get("userId");
			Logger.comment("在码头账号里绑定该微信A");
			Map result = bindWeiXin(token, "mainsite", unionId);	
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			UserCallService.regByWeiXin(unionId, "",true, "登录成功");
			Logger.comment("解绑A微信");
			UserCallService.unbindThirdParty(userId, unionId, ThirdPartyType.WEIXIN);
			UserCallService.checkThirdPartyNeedBindMobile(unionId, ThirdPartyType.WEIXIN, ECode.USER_YES);;
			Logger.comment("绑定A微信");
			result = bindWeiXin(token, "mainsite", unionId);	
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			UserCallService.regByWeiXin(unionId, "",true, "登录成功");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindWeiXinUseroRequestDto_005() {
		Logger.start(true,"用户绑定了A微信账号->解绑A微信账号->绑定B微信账号->绑定成功->用A登录失败 ->用B登录成功");
		try {
			String ali = "NewBindWeixinA_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map weixinUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_Wexin_Identify", "sUnionId", ali);
			Logger.verifyIsNull(weixinUser, "数据库中不存在该第三方账号");
			
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			int userId = (Integer)user.get("userId");
			
			Logger.comment("在码头账号里绑定该微信A");
			Map result = bindWeiXin(token, "mainsite", ali);
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			UserCallService.regByWeiXin(ali, "",true, "登录成功");
			
			Logger.comment("解绑A微信");
			UserCallService.unbindThirdParty(userId, ali, ThirdPartyType.WEIXIN);
			UserCallService.checkThirdPartyNeedBindMobile(ali, ThirdPartyType.WEIXIN, ECode.USER_YES);;
			
			Logger.comment("绑定B微信");
			String aliB = "NewBindWeixinB_"+TestDataManager.getRandomNum(10);
			result = bindWeiXin(token, "mainsite", aliB);	
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			UserCallService.regByWeiXin(aliB, "",true, "登录成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindWeiXinUseroRequestDto_006() {
		Logger.start(true,"未输入token，失败");
		try {
			String ali = "NewBindWeixin_"+TestDataManager.getRandomNum(10);
			Map result = bindWeiXin(null, "mainsite", ali);	
			Logger.verifyEquals(ECode.ERROR400, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
			Logger.verifyEquals(false, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("", result.get("Result"), "验证结果");
			Logger.verifyEquals("错误的请求参数", result.get("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindWeiXinUseroRequestDto_007() {
		Logger.start(true,"未输入业务端类型，成功");
		try {
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			String ali = "NewBindWeixin_"+TestDataManager.getRandomNum(10);
			Map result = bindWeiXin(token, "", ali);	
			Logger.verifyEquals(ECode.ERROR400, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
			Logger.verifyEquals(false, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("", result.get("Result"), "验证结果");
			Logger.verifyEquals("错误的请求参数", result.get("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindWeiXinUseroRequestDto_008() {
		Logger.start(true,"未输入微信账号，失败");
		try {
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			String ali = "NewBindWeixin_"+TestDataManager.getRandomNum(10);
			Map result = bindWeiXin(token, "", ali);	
			Logger.verifyEquals(ECode.ERROR400, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
			Logger.verifyEquals(false, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("", result.get("Result"), "验证结果");
			Logger.verifyEquals("错误的请求参数", result.get("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindWeiXinUseroRequestDto_009() {
		Logger.start(true,"用户A绑定了A微信账号->解绑A微信账号->用户B绑定A微信账号->用户B解绑A微信账号");
		try {
			String phone = TestDataManager.getPhoneNum();
			Logger.comment("注册码头账号A");
			Map userA = UserCallService.registerUser(null, phone,null);
			String tokenA = userA.get("token").toString();
			int userAId = (Integer)userA.get("userId");
			
			Logger.comment("绑定了A微信账号");
			String weixinA = "unbindweixin_"+TestDataManager.getRandomNum(10);
			Map result = bindWeiXin(tokenA, "mainsite", weixinA);
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			
			List<Map> thirdParty = new ArrayList<Map>();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.WEIXIN, weixinA, true);
			UserCallService.checkThirdPartyBindYmt(userAId, thirdParty);
			
			Logger.comment("解绑A微信账号");
			result = UserCallService.unbindThirdParty(userAId, weixinA, ThirdPartyType.WEIXIN);
			Logger.verifyEquals(200d, result.get("ResponseCode"), "验证Code");
			Logger.verifyEquals(true, result.get("Success"), "验证是否成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证Msg信息");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.WEIXIN, weixinA, false);
			UserCallService.checkThirdPartyBindYmt(userAId, thirdParty);
			
			Logger.comment("用户B绑定A微信账号");
			String phoneB = TestDataManager.getPhoneNum();
			Map userB = UserCallService.registerUser(null, phoneB,null);
			String tokenB = userB.get("token").toString();
			int userBId = (Integer)userB.get("userId");
			result = bindWeiXin(tokenB, "mainsite", weixinA);
			
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.WEIXIN, weixinA, true);
			UserCallService.checkThirdPartyBindYmt(userBId, thirdParty);
			
			Logger.comment("解绑A微信账号");
			result = UserCallService.unbindThirdParty(userBId, weixinA, ThirdPartyType.WEIXIN);
			Logger.verifyEquals(200d, result.get("ResponseCode"), "验证Code");
			Logger.verifyEquals(true, result.get("Success"), "验证是否成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证Msg信息");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.WEIXIN, weixinA, false);
			UserCallService.checkThirdPartyBindYmt(userBId, thirdParty);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindWeiXinUseroRequestDto_010() {
		Logger.start(true,"新Token格式微信账号未绑定过，绑定成功JIRACONN-2123");
		try {
			String unionId = "NewBindWeixin_"+TestDataManager.getRandomNum(10);
			
			Logger.comment("前置准备：新创建一个用户");
			UserRegisterInfoRequestDtoBean rgbmbean = UserCallService.initUserRegInfo();
			rgbmbean.setTokenSource("app");
			Map user=UserCallService.registerByMobile(rgbmbean);
			String token = user.get("accesstoken").toString();
			String phone = user.get("phone").toString();
			
			bindweixinuserorequestdtoBean=new BindWeiXinUseroRequestDtoBean();
			bindweixinuserorequestdtoCall=new BindWeiXinUseroRequestDtoCall();
			bindweixinuserorequestdtoBean.setToken(token);
			bindweixinuserorequestdtoBean.setClientType("app");
			WeiXinUserRequestDtoBean wu = createWeiXinUserBean(unionId,TestDataManager.getRandomNum(10));
			wu.setMobile(phone);
			wu.setTokenSource("app");
			bindweixinuserorequestdtoBean.setWeiXinUser(wu);
			bindweixinuserorequestdtoCall.setData(bindweixinuserorequestdtoBean);
			bindweixinuserorequestdtoCall.callService();
			Map result= bindweixinuserorequestdtoCall.getMap();
			
			Logger.verifyEquals(ECode.SUCCESS, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
			Logger.verifyEquals(true, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证结果");
			
			Logger.comment("验证该账号不需要绑定手机");
			UserCallService.checkThirdPartyNeedBindMobile(unionId, ThirdPartyType.WEIXIN, ECode.USER_NO);
			Logger.comment("验证用该账号登陆成功");
			Map loginUser = UserCallService.regByWeiXin(unionId, phone, true, "登录成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
//	@Ignore
	public void Tc_BindWeiXinUseroRequestDto_011() {
		Logger.start(true,"微信老用户数据修复JIRACONN-7439");
		try {
			String unionId = "NewBindWeixin_"+TestDataManager.getRandomNum(10);
			
			Logger.comment("前置准备：新创建一个用户");
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), TestDataManager.getPhoneNum(),null);
			String token = user.get("token").toString();
			
			Map result = bindWeiXin(token, "mainsite", unionId,"329009");
			Logger.verifyEquals(ECode.SUCCESS, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
			Logger.verifyEquals(true, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证结果");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
//	@Test
//	@Category(P1.class)
//	@TestCase
//	public void Tc_BindWeiXinUseroRequestDto_011() {
//		Logger.start(true,"微信账号未绑定过，绑定2个App+Wap成功");
//		try {
//			String unionId = "NewBindWeixin_"+TestDataManager.getRandomNum(10);
//			Logger.comment("验证该账号未绑定过");
//			Map weixinUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_Wexin_Identify", "sUnionId", unionId);
//			Logger.verifyIsNull(weixinUser, "数据库中不存在该第三方账号");
//			
//			Logger.comment("前置准备：新创建一个用户");
//			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
//			String token = user.get("token").toString();
//			
//			Map result = bindWeiXin(token, "App", unionId);
//			Logger.verifyEquals(ECode.SUCCESS, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
//			Logger.verifyEquals(true, result.get("Success"), "验证操作成功");
//			Logger.verifyEquals("ok", result.get("Result"), "验证结果");
//			
//			Logger.comment("验证该账号不需要绑定手机");
//			UserCallService.checkThirdPartyNeedBindMobile(unionId, ThirdPartyType.WEIXIN, ECode.USER_NO);
//			Logger.comment("验证用该账号登陆成功");
//			Map loginUser = UserCallService.regByWeiXin(unionId, "",true, "登录成功");
//			Logger.verifyEquals((Integer)user.get("userId"), ((Double)loginUser.get("userId")).intValue(), "验证绑定的是一个用户");
//			
//			result = bindWeiXin(token, "Wap", unionId);
//			Logger.verifyEquals(ECode.SUCCESS, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
//			Logger.verifyEquals(true, result.get("Success"), "验证操作成功");
//			Logger.verifyEquals("ok", result.get("Result"), "验证结果");
//			
//		} catch (Exception e) {
//			Logger.fail(e);
//		}
//	}
	private WeiXinUserRequestDtoBean createWeiXinUserBean(String weiXinUserId,String openid){
		WeiXinUserRequestDtoBean regBean = new WeiXinUserRequestDtoBean();
		regBean.setCountry("China");
		regBean.setProvince("Shanghai");
		regBean.setCity("Shanghai");
		regBean.setClientIp("1.1.1.1");
		regBean.setHeadimgurl("");
		regBean.setName("WeiXiUser_"+TestDataManager.getRandomNum(5));
		regBean.setOId(openid);
		regBean.setRegSource("app");
		regBean.setSex("Femail");
		regBean.setTimeOut(new Date().getTime());
		regBean.setTSource("pc");
		regBean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
//		regBean.setMobile(phone);
		regBean.setUnionId(weiXinUserId);
		regBean.setWeixinToken("weixintoken_"+TestDataManager.getRandomNum(4));
		return regBean;
	}
	private Map bindWeiXin(String token, String clientType, String weixinUserId) throws ClientProtocolException, IOException, InterruptedException{
		Thread.sleep(1000);
		bindweixinuserorequestdtoBean=new BindWeiXinUseroRequestDtoBean();
		bindweixinuserorequestdtoCall=new BindWeiXinUseroRequestDtoCall();
		bindweixinuserorequestdtoBean.setToken(token);
		bindweixinuserorequestdtoBean.setClientType(clientType);
		bindweixinuserorequestdtoBean.setWeiXinUser(createWeiXinUserBean(weixinUserId,"oid_"+TestDataManager.getRandomNum(8)));
		bindweixinuserorequestdtoCall.setData(bindweixinuserorequestdtoBean);
		bindweixinuserorequestdtoCall.callService();
		return bindweixinuserorequestdtoCall.getMap();
	}
	private Map bindWeiXin(String token, String clientType, String weixinUserId,String openid) throws ClientProtocolException, IOException, InterruptedException{
		Thread.sleep(1000);
		bindweixinuserorequestdtoBean=new BindWeiXinUseroRequestDtoBean();
		bindweixinuserorequestdtoCall=new BindWeiXinUseroRequestDtoCall();
		bindweixinuserorequestdtoBean.setToken(token);
		bindweixinuserorequestdtoBean.setClientType(clientType);
		bindweixinuserorequestdtoBean.setWeiXinUser(createWeiXinUserBean(weixinUserId,openid));
		bindweixinuserorequestdtoCall.setData(bindweixinuserorequestdtoBean);
		bindweixinuserorequestdtoCall.callService();
		return bindweixinuserorequestdtoCall.getMap();
	}
}